package com.spdg.ring.bo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import cn.wolf.base.BaseBo;
import cn.wolf.tools.Log;

import com.spdg.ring.HomeActivity;
import com.spdg.ring.MoreActivity;
import com.spdg.ring.R;
import com.spdg.ring.common.Key;
import com.spdg.ring.datamgr.MenuDataMgr;
import com.spdg.ring.sqlite.model.Menu;

/**
 * <p>Title:  首页底部菜单业务逻辑层</p>
 * <p>Description:  </p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-18             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class MenuTabsBo extends BaseBo {

	private Intent[] intents = new Intent[5];

	private List<View> menus = new ArrayList<View>(5);

	private TabHost mTabHost;

	// 菜单容器
	private LinearLayout mMenuLayout;
	private int mCurrentTabIndex; // 当前菜单序号
	private int count; // 菜单数

	public MenuTabsBo(Activity activity, TabHost tabhost) {
		super(activity);

		this.mTabHost = tabhost;
		init();
	}

	public void init() {

		setupIntent(); // 需先初始化
		setupTabs();

		drawMenuIcon(); //
		drawMenuName();

		switchPage(0); // 默认显示第一个
	}

	/**
	 * 初始化tabs控件
	 */
	private void setupTabs() {
		mMenuLayout = (LinearLayout) findViewById(R.id.menu_layout);
		count = mMenuLayout.getChildCount();
		if (count == 0) {
			Log.w("tabs no one.");
			return;
		}

		final TabHost localTabHost = mTabHost;
		for (int i = 0; i < count; i++) {
			View v = mMenuLayout.getChildAt(i);
			String tag = "menu_tag_" + i;
			v.setTag(tag);
			v.setOnClickListener(new OnTabSpecClickListener(i));
			localTabHost.addTab(localTabHost.newTabSpec(tag).setIndicator(tag)
					.setContent(intents[i]));
			menus.add(v);
		}
	}

	/**
	 * 绘制菜单图片
	 * 1、无sd卡，加载本地默认图片
	 * 2、有sd卡，加载已下载的图片
	 */
	private void drawMenuIcon() {
		if (menus == null || menus.size() == 0)
			return;
		for (int i = 0; i < 5; i++) {
			ViewGroup v = (ViewGroup) menus.get(i);
			ImageView image = (ImageView) v.getChildAt(0);
			image.setImageDrawable(MenuDataMgr.getInstance().getMenuIcon(mActivity, i));
		}
	}

	private void drawMenuName() {
		if (menus == null || menus.size() == 0)
			return;
		for (int i = 0; i < 5; i++) {
			ViewGroup v = (ViewGroup) menus.get(i);
			TextView text = (TextView) v.getChildAt(1);
			text.setText(MenuDataMgr.getInstance().getMenuName(mActivity, i));
		}
	}

	public void switchPage(int index) {
		if (index > menus.size() || index < 0)
			return;
		String tag = menus.get(index).getTag().toString();
		mTabHost.setCurrentTabByTag(tag);
		setCurrentTab(index);

		setMenuBackground(mCurrentTabIndex, null); // 清除背景
		setMenuBackground(index, getDrawable(R.drawable.menu_bg_focused)); // 设置当前菜单背景

		setMenuTextColor(mCurrentTabIndex, getColorStateList(R.color.menu_normal));
		setMenuTextColor(index, getColorStateList(R.color.white));

		setMenuIcon(mCurrentTabIndex);
		setMenuIconFocus(index);

		mCurrentTabIndex = index;
	}

	public void setMenuBackground(int index, Drawable drawable) {
		View v = menus.get(index);
		v.setBackgroundDrawable(drawable);
	}

	public void setMenuTextColor(int index, ColorStateList color) {
		ViewGroup v = (ViewGroup) menus.get(index);
		TextView text = (TextView) v.getChildAt(1);
		text.setTextColor(color);
	}

	public void setMenuIcon(int index) {
		ViewGroup v = (ViewGroup) menus.get(index);
		ImageView image = (ImageView) v.getChildAt(0);
		image.setImageDrawable(MenuDataMgr.getInstance().getMenuIcon(mActivity, index));
	}

	public void setMenuIconFocus(int index) {
		ViewGroup v = (ViewGroup) menus.get(index);
		ImageView image = (ImageView) v.getChildAt(0);
		
		BitmapDrawable drawable = null;
		if (index == 4) {
			drawable = MenuDataMgr.getInstance().getMenuMoreIconFocus(mActivity);
		} else {
			Menu menu = MenuDataMgr.getInstance().getMenu(mActivity, index);
			drawable = MenuDataMgr.getInstance().getMenuIconFocusDrawable(menu);
		}
		
		if (drawable != null) {
			image.setImageDrawable(drawable);
		}
	}

	// 获取当前标签页
	public int getCurPagTabIndex() {
		return mCurrentTabIndex;
	}

	public void setCurrentTab(int index) {
		if (index > menus.size() || index < 0)
			return;
		menus.get(mCurrentTabIndex).setSelected(false);
		View v = menus.get(index);
		v.setSelected(true);
	}

	class OnTabSpecClickListener implements OnClickListener {
		private int index;

		public OnTabSpecClickListener(int index) {
			this.index = index;
		}

		@Override
		public void onClick(View v) {
			switchPage(index);
		}
	}

	private void setupIntent() {
		intents[0] = new Intent(mActivity, HomeActivity.class)
				.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intents[0].putExtra(Key.K_MENU_INFO, MenuDataMgr.getInstance().getMenu(mActivity, 0));

		intents[1] = new Intent(mActivity, HomeActivity.class)
				.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intents[1].putExtra(Key.K_MENU_INFO, MenuDataMgr.getInstance().getMenu(mActivity, 1));

		intents[2] = new Intent(mActivity, HomeActivity.class)
				.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intents[2].putExtra(Key.K_MENU_INFO, MenuDataMgr.getInstance().getMenu(mActivity, 2));

		intents[3] = new Intent(mActivity, HomeActivity.class)
				.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intents[3].putExtra(Key.K_MENU_INFO, MenuDataMgr.getInstance().getMenu(mActivity, 3));

		intents[4] = new Intent(mActivity, MoreActivity.class)
				.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
	}
}
